def DFS(Adj):
    V=xrange(len(Adj))
    color=['white']*len(Adj)
    pi=[None]*len(Adj)
    d=[0]*len(Adj)
    f=[0]*len(Adj)
    time=0
    stk=[]
    for u in V:
        if color[u]=='white':
            stk.append(u)
            while(len(stk)>0):
                w=stk.pop()
                if color[w]=='white':
                    color[w]='grey'
                    time=time+1
                    d[w]=time
                    stk.append(w)#push back
                finished=True
                for v in Adj[w]:
                    if color[v]=='white':
                        finished=False
                        pi[v]=w
                        stk.append(v)
                if finished==True: #and color[w]=='grey':
                    color[w]='black'
                    time=time+1
                    f[w]=time
    return pi

def printpath(pi,s,v):
    if v==s:
        print s
    elif pi[v]==None:
        print 'No path exist from',s,'to',v
    else:
        printpath(pi,s,pi[v])
        print v

if __name__=='__main__':
    Adj=[[1,2,3],[0,2],[0,1],[0]]
    print Adj
    pi=DFS(Adj)
    print pi
    printpath(pi,1,2)
    
                
